package session.stateless;

import entity.Address;
import entity.BatchRequired;
import entity.Customer;
import entity.Product;
import entity.ProductionBatch;
import entity.PurchaseItem;
import entity.PurchaseOrder;
import java.lang.Double;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class PurchaseOrderSessionBean implements PurchaseOrderSessionBeanLocal, PurchaseOrderSessionBeanRemote {
    

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Long addPurchaseOrder(String poNumber, Double totalAmount, Double cost, Double shippingCost,
            String status, Date poDate, String remark, Long bId, Long custId,
            Collection<PurchaseItem> purchaseItems, Double creditRate, Double firstPayment) {


        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setPoNumber(poNumber);
        purchaseOrder.setTotalAmount(totalAmount);
        purchaseOrder.setStatus(status);
        purchaseOrder.setPoDate(poDate);
        purchaseOrder.setCost(cost);
        purchaseOrder.setShippingCost(shippingCost);
        purchaseOrder.setRemark(remark);

        purchaseOrder.setPurchaseItems(new ArrayList<PurchaseItem>());
        for (PurchaseItem poi : purchaseItems) {
            PurchaseItem temp = new PurchaseItem(poi.getUnitPrice(), poi.getShippingRate(), poi.getSubTotal(), poi.getQuantity(), poi.getDiscount(), null, poi.getRequestDate(), null, poi.getStatus(), poi.getOutstandingAmount());
            Product p = entityManager.find(Product.class, poi.getProduct().getCode());
            Address ad = entityManager.find(Address.class, poi.getShippingAddress().getId());
            temp.setProduct(p);
            temp.setShippingAddress(ad);
            purchaseOrder.getPurchaseItems().add(temp);
        }
        purchaseOrder.setCreditRate(creditRate);
        purchaseOrder.setFirstPayment(firstPayment);
        Customer customer = entityManager.find(Customer.class, custId);
        Address billingAddress = entityManager.find(Address.class, bId);
        purchaseOrder.setCustomer(customer);
        purchaseOrder.setBillingAddress(billingAddress);
        entityManager.persist(purchaseOrder);
        entityManager.flush();

        return purchaseOrder.getId();

    }

    @Override
    public void updatePurchaseOrder(Long id, String status) {
        PurchaseOrder purchaseOrder = entityManager.find(PurchaseOrder.class, id);
        purchaseOrder.setStatus(status);
        entityManager.persist(purchaseOrder);
        entityManager.flush();
    }

    @Override
    public void updatePurchaseItemStatus(Long id, String status) {
        PurchaseItem poi = entityManager.find(PurchaseItem.class, id);
        poi.setStatus(status);
        entityManager.persist(poi);
        entityManager.flush();

    }

    @Override
    public void addPurchaseItemBatch(Long id, BatchRequired batch) {
        ProductionBatch pb = entityManager.find(ProductionBatch.class, batch.getProductionBatch().getId());
        BatchRequired br = new BatchRequired(batch.getQuantity(), pb);
        entityManager.persist(br);
        entityManager.flush();
        PurchaseItem poi = entityManager.find(PurchaseItem.class, id);
        poi.getBatchesRequired().add(br);
        entityManager.persist(poi);
        entityManager.flush();
    }


    @Override
    public ArrayList<PurchaseItem> sortPurchaseItems(ArrayList<PurchaseItem> purchaseItems) {
        ArrayList<PurchaseItem> pois = new ArrayList(purchaseItems);
        ArrayList<PurchaseItem> sortedPois = new ArrayList();
        Calendar cal = Calendar.getInstance();
        PurchaseItem earliest = new PurchaseItem();

        HashMap hs = new HashMap();
        System.out.println(pois.size());
        System.out.println(sortedPois.size());
        while (sortedPois.size() < pois.size()) {
            cal.set(3000, 0, 1);
            for (PurchaseItem pi : pois) {
                Calendar next = Calendar.getInstance();
                next.setTime(pi.getRequestDate());
                if (next.before(cal) && hs.get(pi) == null) {
                    earliest = pi;
                    cal = next;
                }
            }
            sortedPois.add(earliest);
            hs.put(earliest, 0);
        }

        System.out.println("DATA FOR ATP");
        for (PurchaseItem pi : sortedPois) {
            System.out.println(pi.getRequestDate());
        }
        return sortedPois  ;
        
    }

    @Override
    public PurchaseOrder getPurchaseOrder(Long purchaseId){
    
        PurchaseOrder purchaseOrder = entityManager.find(PurchaseOrder.class, purchaseId);
        return purchaseOrder;
    
    } 
}
